package com.lightsaber.trade.cloth.web.planthird;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.web.BaseAction;
import com.lightsaber.trade.cloth.entity.planning.AddResourceGroup;
import com.lightsaber.trade.cloth.entity.planning.AddResourceGroupItem;
import com.lightsaber.trade.cloth.entity.planning.AdminDuty;
import com.lightsaber.trade.cloth.entity.planning.Level3Planning;
import com.lightsaber.trade.cloth.entity.planning.ResourceGroup;
import com.lightsaber.trade.cloth.entity.planning.ResourceGroupItem;
import com.lightsaber.trade.cloth.entity.planning.ResourceGroupPage;
import com.lightsaber.trade.cloth.service.param.ParamService;
import com.lightsaber.trade.cloth.service.planning.AdminDutyService;
import com.lightsaber.trade.cloth.service.planning.Level3PlanningService;
import com.lightsaber.trade.cloth.service.planning.ResourceGroupItemService;
import com.lightsaber.trade.cloth.service.planning.ResourceGroupService;

@Namespace("/planthird")
public class PlanThirdAction extends BaseAction {
    private static final long serialVersionUID = -7076736990225879052L;

    private static final Logger log = LoggerFactory.getLogger(PlanThirdAction.class);

    public static final String SUCESSFULLY = "<Grid><IO Result='0'/></Grid>";

    @Autowired
    private transient Level3PlanningService level3Service;
    @Autowired
    private transient ParamService paramService;
    @Autowired
    private transient ResourceGroupService resourceGroupService;
    @Autowired
    private transient ResourceGroupItemService resourceGroupItemService;

    @Autowired
    private transient AdminDutyService adminDutyService;
    private Map<Integer, String> plannerYearMap;

    private Level3Planning level3Planning;

    @Value(value = "#{appProperties['category.page.size']}")
    private String pageSize;

    private Page<Level3Planning> page;
    private boolean search;

    private String studyMode;
    private String campus;
    private String department;

    private String programmePlanningItemId;
    private String viewDetailData;

    private String dutyId;
    private String curriculumHours;

    private ResourceGroupPage resourceGroupPage;

    @Override
    public String execute() {
        return list();
    }

    public String list() {
        if (search) {
            if (page == null) {
                page = new Page<Level3Planning>(Integer.valueOf(pageSize));
            } else {
                page.setPageSize(Integer.valueOf(pageSize));
            }

            if (!page.isOrderBySetted()) {
                page.setOrderBy(BaseEntity.ID);
                page.setOrder(Page.ASC);
            }

            level3Service.findPage(level3Planning, page);
        } else {
            level3Planning = null;
        }
        setPlannerYearMap(level3Service.getAllPlanningExercise());
        return SUCCESS;
    }

    @Override
    public String input() throws Exception {
        level3Planning = level3Service.get(level3Planning.getId());
        return INPUT;
    }

    public String save() throws Exception {
        level3Service.save(level3Planning);
        addActionMessage(getText(PROCESSED_SUCCESS));
        return input();
    }

    public String toViewDetail() throws Exception {
        setLevel3Planning(level3Planning);
        setStudyMode(studyMode);
        setCampus(campus);
        setDepartment(department);
        return "view-detail";
    }

    public void getViewDetailData() throws Exception {
        String str = level3Service.getViewDetail(level3Planning.getId(), studyMode, campus,
                department, false);
        str = str.replace("(Resource Group)", "&lt;br/&gt;Resource &lt;br/&gt;Group");
        str = str.replace("(Total Curriculum Hours)",
                "Total &lt;br/&gt;Curriculum &lt;br/&gt;Hours");
        str = str.replace("(Total Planned Place)", "Total &lt;br/&gt;Planned &lt;br/&gt;Place");
        log.info("replace : {}", str);
        Struts2Utils.renderXml(str);
    }

    public String turnToAddResourcePage() {
        // if (resourceGroupPage == null) {
        AdminDuty adminDuty = adminDutyService.findById(dutyId);
        resourceGroupPage = new ResourceGroupPage();
        resourceGroupPage.setDuty(String.valueOf(adminDuty.getDuty()));
        resourceGroupPage.setCurriculumHours(curriculumHours);
        resourceGroupPage.setLevel3PlanningId(level3Planning.getId());
        resourceGroupPage.setProgrammePlanningItemId(programmePlanningItemId);
        resourceGroupPage.setCampus(adminDuty.getOperatingCampus());
        resourceGroupPage.setDepartment(adminDuty.getOperatingUnit());
        // }
        List<ResourceGroup> list = resourceGroupService.findByLeve3IdAndProgrammePlanningId(
                level3Planning.getId(), programmePlanningItemId);
        List<AddResourceGroup> argsList = new ArrayList<AddResourceGroup>();
        for (ResourceGroup rg : list) {
            AddResourceGroup arg = new AddResourceGroup();
            arg.setName(rg.getName());
            arg.setPlace(String.valueOf(rg.getPlace()));
            arg.setId(rg.getId());
            List<ResourceGroupItem> rgiList = rg.getResourceGroupItems();
            List<AddResourceGroupItem> argisList = new ArrayList<AddResourceGroupItem>();
            for (ResourceGroupItem rgi : rgiList) {
                AddResourceGroupItem argi = new AddResourceGroupItem();
                argi.setCurriculumHours(String.valueOf(rgi.getCurriculumHours()));
                argi.setDistribution(String.valueOf(rgi.getDistribution()));
                argi.setServingCampus(rgi.getServingCampus());
                argi.setServingDept(rgi.getServingDept());
                argisList.add(argi);
            }
            arg.setResourceGroupItems(argisList);
            argsList.add(arg);
        }
        resourceGroupPage.setResourceGroups(argsList);
        if (argsList.isEmpty()) {
            resourceGroupPage.setResourceGroupSize(0);
        } else {
            resourceGroupPage.setResourceGroupSize(argsList.size());
        }
        setResourceGroupPage(resourceGroupPage);

        return "resource-group";
    }

    public void saveViewDetail() {
        level3Service.saveViewDetail(viewDetailData);
        Struts2Utils.renderXml(SUCESSFULLY);
    }

    public void confirmForDetail() throws Exception {
        level3Service.confirmForDetail(level3Planning.getId());
        // Struts2Utils.renderXml("");
    }

    public String saveResourceGroup() {
        resourceGroupService.saveResourceGroupList(resourceGroupPage);
        List<ResourceGroup> list = resourceGroupService.findByLeve3IdAndProgrammePlanningId(
                resourceGroupPage.getLevel3PlanningId(),
                resourceGroupPage.getProgrammePlanningItemId());
        List<AddResourceGroup> argsList = new ArrayList<AddResourceGroup>();
        for (ResourceGroup rg : list) {
            AddResourceGroup arg = new AddResourceGroup();
            arg.setName(rg.getName());
            arg.setPlace(String.valueOf(rg.getPlace()));
            arg.setId(rg.getId());
            List<ResourceGroupItem> rgiList = resourceGroupItemService.findByResourceGroupId(rg
                    .getId());
            List<AddResourceGroupItem> argisList = new ArrayList<AddResourceGroupItem>();
            for (ResourceGroupItem rgi : rgiList) {
                AddResourceGroupItem argi = new AddResourceGroupItem();
                argi.setCurriculumHours(String.valueOf(rgi.getCurriculumHours()));
                argi.setDistribution(String.valueOf(rgi.getDistribution()));
                argi.setServingCampus(rgi.getServingCampus());
                argi.setServingDept(rgi.getServingDept());
                argisList.add(argi);
            }
            arg.setResourceGroupItems(argisList);
            argsList.add(arg);
        }
        resourceGroupPage.setResourceGroups(argsList);
        if (argsList.isEmpty()) {
            resourceGroupPage.setResourceGroupSize(0);
        } else {
            resourceGroupPage.setResourceGroupSize(argsList.size());
        }
        setResourceGroupPage(resourceGroupPage);
        addActionMessage(getText(PROCESSED_SUCCESS));
        return "resource-group";

    }

    public Map<Integer, String> getPlannerYearMap() {
        return plannerYearMap;
    }

    public void setPlannerYearMap(final Map<Integer, String> plannerYearMap) {
        this.plannerYearMap = plannerYearMap;
    }

    public Level3Planning getLevel3Planning() {
        return level3Planning;
    }

    public void setLevel3Planning(final Level3Planning level3Planning) {
        this.level3Planning = level3Planning;
    }

    public String getPageSize() {
        return pageSize;
    }

    public void setPageSize(final String pageSize) {
        this.pageSize = pageSize;
    }

    public Page<Level3Planning> getPage() {
        return page;
    }

    public void setPage(final Page<Level3Planning> page) {
        this.page = page;
    }

    public boolean isSearch() {
        return search;
    }

    public void setSearch(final boolean search) {
        this.search = search;
    }

    public String getStudyMode() {
        return studyMode;
    }

    public void setStudyMode(final String studyMode) {
        this.studyMode = studyMode;
    }

    public String getCampus() {
        return campus;
    }

    public void setCampus(final String campus) {
        this.campus = campus;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(final String department) {
        this.department = department;
    }

    public ParamService getParamService() {
        return paramService;
    }

    public void setParamService(final ParamService paramService) {
        this.paramService = paramService;
    }

    public String getProgrammePlanningItemId() {
        return programmePlanningItemId;
    }

    public void setProgrammePlanningItemId(final String programmePlanningItemId) {
        this.programmePlanningItemId = programmePlanningItemId;
    }

    public void setViewDetailData(final String viewDetailData) {
        this.viewDetailData = viewDetailData;
    }

    public String getDutyId() {
        return dutyId;
    }

    public void setDutyId(final String dutyId) {
        this.dutyId = dutyId;
    }

    public ResourceGroupPage getResourceGroupPage() {
        return resourceGroupPage;
    }

    public void setResourceGroupPage(final ResourceGroupPage resourceGroupPage) {
        this.resourceGroupPage = resourceGroupPage;
    }

    public String getCurriculumHours() {
        return curriculumHours;
    }

    public void setCurriculumHours(final String curriculumHours) {
        this.curriculumHours = curriculumHours;
    }

}
